define([
    'base',
    'bootstrapDialog',
    'bootstrapTable',
    'plugins/ztree/ztree'
], function (base,BootstrapDialog) {
    //默认加载基础库
    base();

  
    return {
        list: function () {
            var myTable = $('#dataList');
            
            var tableRefresh = function () {
                myTable.bootstrapTable('refresh', {url: 'api.json'})
            };
            
            //table按需刷新动作
            var doAction = function (obj) {
            	if(obj.pid==0){
            		  myTable.bootstrapTable('refresh', {url: 'api.json'})
            	}else{
            		myTable.bootstrapTable('refresh', {url: 'api.json?orgId=' + obj.id})
            	}
            };

            //table当前行的响应事件
            window.operateEvents = {
                    'click .update': function (e, value, row) {
                 	   winModal({ 
                 		   row: row,
                 		   title:'部门事项',
                 		   btName:'保存'
                 	   });
                 },
                'click .status': function (e, value, row) {
                    $.baseUtils.oneStatus({
                        row: row,
                        tableName: myTable,
                        nameText: '组织机构',
                        nameValue: row.orgName,
                        url: '../status/action'
                    });
                },
                'click .delete': function (e, value, row) {
                    $.baseUtils.oneDelete({
                        row: row,
                        tableName: myTable,
                        nameText: '组织机构',
                        nameValue: row.orgName,
                        url: '../delete/action'
                    });
                }
            };

            //table表格
            myTable.bootstrapTable({
                url: 'api.json', // 接口 URL 地址
                cache: false, // 不缓存
                striped: true, // 隔行加亮
                pagination: true, // 开启分页功能
                sidePagination: 'server',//服务端分页
                sortOrder: 'desc', // 设置排序为反序 desc
                fixedColumns: true,//自动修复
                queryParams: queryParams,//搜索附加参数
                smartDisplay: false, // 智能显示 pagination 和 cardview 等
                toolbar: ".toolBar", //顶部工具菜单
                toolbarAlign: "right", //顶部工具位置
                mobileResponsive: "true", //手机模式自适应宽度（自动变成卡片模式）
                columns: [
                    {
                        field: 'id',
                        title: '编号',
                        align: 'center',
                        width: '5%'
                    }, {
                        field: 'orgName',
                        title: '部门名称',
                        align: 'center',
                        width: '15%'
                    }, {
                        field: 'title',
                        title: '事项名称',
                        align: 'center',
                        width: '25%'
                    },
                    {
                        field: 'status',
                        title: '状态',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var result = "";
                            switch (value) {
                                case 0:
                                    result = "<span class=\"label label-danger\"><i class=\"fa fa-lock\" aria-hidden=\"false\"></i>&nbsp;禁用</span";
                                    break;
                                case 1:
                                    result = "<span class=\"label label-primary\"><i class=\"fa fa-unlock\" aria-hidden=\"true\"></i>&nbsp;启用</span";
                                    break;
                                default:
                                    break;
                            }
                            return result;
                        },
                        width: '5%'
                    },
                    {
                    	field: 'action',
                        title: '操作',
                        align: 'center',
                        width: '15%',
                        formatter: operateFormatter,
                        events: operateEvents
                    }
                ]
            });

            // 初始化slimscroll右栏
            $('#sideTree').slimScroll({
                height: 580,
                railOpacity: 0.4,
                wheelStep: 5,
                color: '#18212A',
                opacity: .5,
                railVisible: true
            });


            //搜索刷新
            $('#search').on('click', function () {
                tableRefresh();
            });

            //toolbar按钮相应事件
            //新增        
        $(".toolbar-save").on('click', function () {
        	 winModal({ 
      		   title:'部门事项',
      		   btName:'保存'
      	   });
        });
        
        
            //刷新
            $(".toolbar-refresh").on('click', function () {
                tableRefresh();
            });

       

            //搜索参数传递
            function queryParams(params) {
                return {
                    limit: params.limit,
                    offset: params.offset
                };
            }

            //回显
            function responseHandler(res) {
                return res.rows;
            }

            
            //模态框
            function winModal(obj) {
                BootstrapDialog.show({
                    type: BootstrapDialog.TYPE_DEFAULT,
                    tabindex: null,//解决select2搜索框失去焦点的问题
                    title: obj.title,
                    closable: true,	//关闭箭头启动
                    closeByBackdrop: false,	//点击空白不消失
                    closeByKeyboard: true,//ESC取消禁用
                    message: $('<div></div>').load('../save/form.html'),
                    data: { 
                        'row': obj.row
                    },
                    buttons: [{
                        label: '取消',
                        cssClass: 'btn-white cancel',
                        icon: 'fa fa-times',
                        action: function (dialog) {
                            dialog.close();
                        }
                    }, {
                        label: obj.btName,
                        //hotkey: 13,//回车提交
                        cssClass: 'btn-primary',
                        icon: 'fa fa-check',
                        action: function (dialog) {
                            //返回验证结果
                            var flag = false;
                            flag = $("#winModal").validate().form();
                            if (flag) {
                                $.baseUtils.ajax({
                                    dialog: dialog,
                                    url: '../save/action',
                                    data: $("#winModal").serialize(),
                                    text: "保存",
                                    isTable: true,
                                    tableName: '#dataList',
                                    isDialog: true
                                })
                            }
                        }
                    }],
                    onshown: function (dialog) {
                    	var row=obj.row;
                        if (row != null) {
                            $('#id').val(row.id);
                            $('#title').val(row.title);
                            $.post('orgList.json', {},
                                    function (data) {
                                        if (data.success) {
                                        	var  orgId_back=  $("#orgId_back").val();
                                            var selectHtml = "<option value=''>请选择</option>";
                                            $.each(data.rows, function (i, org) {
                                            	if(row.orgId==org.id){
                                            		selectHtml += "<option value='" + org.id + "'  selected  >" + org.orgName + "</option>";	
                                            	}else{
                                            		selectHtml += "<option value='" + org.id + "'  >" + org.orgName + "</option>";
                                            	}
                                            });
                                            $("#orgId_save").html(selectHtml);
                                        }
                                    }, "json");
                        }
                    }
                });
            }
            

            //左侧资源导航ztree插件
            $.baseUtils.tree({
                treeNodeName: "#navTree",
                ajaxUrl: "tree/0",
                isAction: true,
                doAction: doAction
            });

            //ztree插件链接点击刷新全部
            $("#allResList").on('click', function () {
                tableRefresh();
            });

        },
        save: function () {
            $.post('orgList.json', {},
                    function (data) {
                        if (data.success) {
                        	var  orgId_back=  $("#orgId_back").val();
                            var selectHtml = "<option value=''>请选择</option>";
                            $.each(data.rows, function (i, org) {
                            	if(orgId_back==org.id){
                            		selectHtml += "<option value='" + org.id + "'  selected  >" + org.orgName + "</option>";	
                            	}else{
                            		selectHtml += "<option value='" + org.id + "'  >" + org.orgName + "</option>";
                            	}
                            });
                            $("#orgId_save").html(selectHtml);
                        }
                    }, "json");
        }
    }
});






